package gc;

/**
 * 此代码演示了两点：
 * 1.对象可以在被GC时自我拯救
 * 2.机会只有一次，因为一个对象的finalize()方法最多只会被系统自动调用一次
 * -verbose:gc -XX:+PrintGCDetails
 * @author Ditto
 *
 */
public class FinalizeEscapeGC {

	public static FinalizeEscapeGC SAVE_HOOK = null;
	
	public void isAlive(){
		System.out.println("yes,i am still alive");
	}
	
	protected void finalize() throws Throwable {
		super.finalize();
		System.out.println("finalize method executed");
		FinalizeEscapeGC.SAVE_HOOK = this;
	}
	
	public static void main(String[] args) throws InterruptedException {
		SAVE_HOOK = new FinalizeEscapeGC();
		
		SAVE_HOOK=null;
		System.gc();
		Thread.sleep(500);
		if(SAVE_HOOK !=null){
			SAVE_HOOK.isAlive();
		}else{
			System.out.println("no, i am dead:(");
		}
		
		SAVE_HOOK=null;
		System.gc();
		Thread.sleep(500);
		if(SAVE_HOOK !=null){
			SAVE_HOOK.isAlive();
		}else{
			System.out.println("no, i am dead:(");
		}
				
		
	}
}
